package com.leyou.cart.filter;

import com.leyou.auth.pojo.UserInfo;
import com.leyou.auth.utils.JwtUtils;
import com.leyou.cart.config.JwtProperties;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.CookieUtils;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@EnableConfigurationProperties(JwtProperties.class)
public class LoginInterceptor extends HandlerInterceptorAdapter {

    private  JwtProperties prop;
    public LoginInterceptor() {
        super();
    }
    public  LoginInterceptor(JwtProperties prop){
        this.prop=prop;
    }
    //定义一个线程域,存放登录用户,解决线程安全问题
    private static  final ThreadLocal<UserInfo> th = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        try{
            //获取cookie中的token
            String token = CookieUtils.getCookieValue(request, prop.getCookieName());
            //解析成功,证明已登录
            UserInfo userInfo = JwtUtils.getInfoFromToken(token, prop.getPublicKey());
            //放入线程域
            th.set(userInfo);
            return  true;
        }catch (Exception e){
            //抛出异常,证明未登录,返回401
            throw  new LyException(ExceptionEnum.UNAUTHORIZED);
        }

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
       th.remove();;
    }

    //提供静态方法获取线程域中的user信息
    public static UserInfo getLoginUser() {
        return th.get();
    }
}
